<template>
  <avue-crud
    :option.sync="optionFillingStationList"
    :table-loading="loading"
    :data="data"
    :page="page"
    @row-del="rowDel"
    v-model="form"
    :permission="permissionList"
    :before-open="beforeOpen"
    @search-change="searchChange"
    @search-reset="searchReset"
    @selection-change="selectionChange"
    @current-change="currentChange"
    @size-change="sizeChange"
    @on-load="onLoad"
    row-class-name="textCut"
  >
    <template #menuLeft>
      <el-form :inline="true" :model="query" class="queryForm">
        <el-form-item>
          <el-select
            size="small"
            v-model="query.siteType"
            placeholder="加注站类型"
            clearable
          >
            <el-option
              :label="item.dictValue"
              :value="item.dictKey"
              v-for="item in siteTypeList"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select
            size="small"
            v-model="query.siteStatus"
            placeholder="营业状态"
            clearable
          >
            <el-option label="已关闭" value="0"></el-option>
            <el-option label="正常" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            size="small"
            suffix-icon="el-icon-search"
            v-model="query.siteName"
            placeholder="请输入加注站名称"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
    </template>
    <template slot="menuRight">
      <el-button
        size="small"
        type="primary"
        @click="addRow"
        v-if="permission.site_add"
        >新增加注站</el-button
      >
    </template>
  </avue-crud>
</template>

<script>
import { isTrue } from "@/util/common-util";
import { detail, getList, remove } from "@/api/stake/filling-stations";
import { mapGetters } from "vuex";
import { optionFillingStationList } from "@/const/stake/filling-stations";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      optionFillingStationList,
      data: [],
    };
  },
  props: {
    siteTypeList: {
      type: Array,
      default: [],
    },
  },
  watch: {
    query: {
      immediate: true,
      deep: true,
      handler(val) {
        console.log("----search-----", val);
        console.log(this.permission);
        this.query = val;
        this.onLoad(this.page, val);
      },
    },
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        viewBtn: isTrue(this.permission.site_view),
        delBtn: isTrue(this.permission.site_delete),
        editBtn: isTrue(this.permission.site_edit),
      };
    },
  },
  methods: {
    addRow() {
      this.$emit("add");
    },
    rowDel(row) {
      this.$confirm("确定将" + row.siteName + "删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    refresh() {
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (type == "edit") {
        this.$emit("edit", this.form.id);
      } else {
        this.$emit("view", this.form.id);
      }
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        data.records.forEach((item) => {
          item.siteType = Number(item.siteType);
        });
        this.page.total = data.total;

        this.data = data.records;
        console.log(this.data);
        this.loading = false;
      });
    },
  },
};
</script>

<style></style>
